import wilberforce.events.simpleEventHelper;
import wilberforce.ui.forms.ListBox;
import com.bourre.commands.Delegate;
import wilberforce.util.textField.textFieldUtility;
import wilberforce.util.drawing.drawingUtility;
import wilberforce.events.UIEvents;
import com.bourre.events.BasicEvent;
import com.bourre.events.EventType;

/**
 * @author Simon
 */
class wilberforce.ui.forms.ComboBox extends simpleEventHelper {
	
	private var container:MovieClip;	private var listBoxContainer:MovieClip;
	
	private var dataArray:Array;
	public var selectedIndex:Number;
	private var listBoxInstance:ListBox;
	private var selectedLabelTextField:TextField;
	private var labelMC:MovieClip;
	//private var backgroundMC:MovieClip;
	//private var initialised:Boolean;
	
	private var _width;
	private var height:Number=22;
	
	private var rightGap=15;
	private var expanded:Boolean;
	
	private var listBoxHeight:Number=100;

	
	private static var margin:Number=5;
	
	private var mouseListener;
	
	private static var COMBO_BOX_ARROW_ATTACHNAME:String="ComboBoxArrow";
	
	public function ComboBox(container:MovieClip,tWidth:Number) {
		super();
		_width=tWidth;
		this.container=container;
		
		if (_width==null) _width=170;
		
		//backgroundMC=container.createEmptyMovieClip("backgroundMC",container.getNextHighestDepth());
		labelMC=container.createEmptyMovieClip("labelMC",container.getNextHighestDepth());		listBoxContainer=container.createEmptyMovieClip("listBoxContainer",container.getNextHighestDepth());
		
		renderComboBox();
		
		//var tData={_x:margin,_y:height+margin};
		
		listBoxContainer._x=margin;		listBoxContainer._y=height;
		listBoxInstance=new ListBox(listBoxContainer);//this.attachMovie("sceeListBox","listMc",this.getNextHighestDepth(),tData)
		listBoxInstance.addListener(this);
		listBoxContainer._visible=false;
		//backgroundMC._visible=false;
		
		/*
		backgroundMC.onPress=function() {
			
		}
		backgroundMC.useHandCursor=false;	
		 */
		expanded=false;
		 		
		labelMC.onPress=Delegate.create(this,toggleExpanded);
		
		
		dataArray=[];
		selectedIndex=-1;
		
		
		mouseListener=new Object;
		Mouse.addListener(mouseListener);
		mouseListener.onMouseDown=Delegate.create(this,mouseClicked);
					
		listBoxInstance.setSize(_width-margin*2,listBoxHeight);
		// Do we already have data?
		if (dataArray.length>0) {			
			listBoxInstance.setData(dataArray,selectedIndex);
		}
	}
	
	/** Tests the position of the mouse (to see if it should be closed) */
	function mouseClicked() {
		//trace("CONMBO CLICKED "+tx+","+ty);
		var tx:Number=container._xmouse;
		var ty:Number=container._ymouse;
		if (expanded) {
			if (tx<0 || tx>container._width || ty<0 || ty>container._height) {
				// Close
				toggleExpanded();
			}
		}
	}
	
	/** Toggles the state of the component, to display or hide the listBox */
	function toggleExpanded() {
		expanded=!expanded;
		if (!expanded) {
			listBoxContainer._visible=false;
			//backgroundMC._visible=false
		}
		else {
			listBoxContainer._visible=true;
			//backgroundMC._visible=true
		}
	}
	

	
	/** Called when an item within the listBox is selected  */	
	function onComponentSelectionChanged(e:BasicEvent)
	{
	
		var index:Number=listBoxInstance.selectedIndex;
		//index,tData) {
		
		toggleExpanded();
		setSelectedIndex(index);
		
		
		dispatchEvent(new BasicEvent(UIEvents.UI_COMPONENT_SELECTION_CHANGED,this));
		/*
		for (var i in listenerObjects) {
			listenerObjects[i].comboBoxSelected(index,tData)
		}		 
		 */
	}
	
	public function selectByData(value:Object)
	{
		for (var i=0;i<dataArray.length;i++)
		{
			if (dataArray[i].data==value)
			{
				setSelectedIndex(i);
				trace("FOund matching index");
				return;
			}
		}
	}
	
	public function getValue()
	{
		return dataArray[selectedIndex].data;
	}
	public function setSelectedIndex(index:Number)
	{
		listBoxInstance.setSelectedIndex(index);
		selectedIndex=index;
		// Update the label
		var tLabel=dataArray[selectedIndex].label;
		
		selectedLabelTextField._width=_width-rightGap;
		trace("Setting text to "+tLabel+" for "+selectedLabelTextField);
		selectedLabelTextField.text=tLabel;
		selectedLabelTextField.setTextFormat(ListBox.labelTextFormat);
	}
	
	
	/** Allows the data for the component to be set  */	
	function setData(tArray:Array,tSelectedIndex:Number) {
		
		if (tSelectedIndex==null || tSelectedIndex==undefined) tSelectedIndex=0;
		dataArray=tArray;
		selectedIndex=tSelectedIndex;
		
		// Get the correct data		
		var tLabel=dataArray[selectedIndex].label;
		
		selectedLabelTextField=textFieldUtility.createTextField(labelMC,3,1,_width-rightGap,height,ListBox.labelTextFormat,tLabel,false,false,true);
		selectedLabelTextField._height=ListBox.labelTextFormat.size+5;
		//selectedLabelTextField.multiline=false;		
		listBoxInstance.setData(tArray,tSelectedIndex);		
	}
	
	public function addItem(tLabel:String,tData:Object):Void {
		//var tDataItem={label:tLabel,data:tData}
		//dataItems.push(tDataItem);
		listBoxInstance.addItem(tLabel,tData);		
	}
	
	
	
	/** Renders the background of the box */	
	function renderComboBox() {
		
		// Draw left section
		var tRoundedness=10;
		//drawingUtility.drawRoundedRectangleGradientFill(labelMC,0,0,width-rightGap-1,height,tRoundedness,0x000000,0,0xFFFFFF,100,0xE2E2E2,100,0,false,true);
		//drawingUtility.drawRoundedRectangleGradientFill(labelMC,width-rightGap,0,rightGap,height,tRoundedness,0x000000,0,0xFFFFFF,100,0xE2E2E2,100,0,true,false);
		
		
		drawingUtility.drawRectangle(labelMC,0,0,_width,height,0x666666,100,0xFFFFFF,0,100);
		/*
		labelMC.moveTo(_width-rightGap-1,0);
		labelMC.lineStyle(1,0xB8B8B8,100);
		labelMC.lineTo(_width-rightGap-1,height);
		
		 *
		 */
		// Attach the arrow
		//var tData={_x:_width-rightGap/2,_y:height/2};
		var tComboArrowlabelMC:MovieClip=labelMC.attachMovie(COMBO_BOX_ARROW_ATTACHNAME,"arrow",labelMC.getNextHighestDepth());
		tComboArrowlabelMC._x=_width-tComboArrowlabelMC._width;
		//drawingUtility.drawRoundedRectangleGradientFill(backgroundMC,-1,-1,width+2,2+height+listBoxHeight+margin*2,tRoundedness,0x000000,0,0x000000,30,0x000000,30,0,false,false)
		//drawingUtility.drawRoundedRectangleGradientFill(backgroundMC,0,0,width,height+listBoxHeight+margin*2,tRoundedness,0x000000,30,0xFFFFFF,100,0xE2E2E2,100,0,false,false)
		
		
	}

}